package com.ea.eadp.nexus.test.pageDefinition;

import com.ea.eadp.nexus.test.common.PageOperation;
import com.ea.eadp.nexus.test.common.TestUtil;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;

/**
 * Created with IntelliJ IDEA.
 * User: hzeng
 * Date: 1/23/15
 * Time: 3:07 PM
 * To change this template use File | Settings | File Templates.
 */
public class SignupPageWeb3 extends Signup_Web {
    private String uri;
    private static String emailInputBox = "email";
    public static String passwordInputBox = "password";
    public static String OriginIdInputBox = "originId";
    public static String DobInputBox = "dob";
    public static String RecaptchaInputBox = "recaptcha";

    private static String contactMeID = "contactMe";
    private static String readAcceptID = "readAccept";
    private static String shareInfoID = "shareInfo";
    private static String EmailExisted_MESSAGE = "An EA Account already exists for this email address. Try logging in.";

    public SignupPageWeb3(WebDriver driver) {
        super(driver);
    }

    public void navigate(String uri) {
        this.uri = uri;
        navigate();
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    @Override
    public void clickToSignupBasicS1() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.cssSelector(".btn-next>span")).click();
    }

    public void clickToSignupBasicS2() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.cssSelector(".btn-create>span")).click();
        identifierId = "firstName";
        PageOperation.WebDriverWait(driver, identifierId);
    }

    public void clickToSignupBasicS3() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.cssSelector(".btn-profile>span")).click();
    }

    public boolean clickToCheckIfEmailExisted() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.cssSelector(".btn-next>span")).click();
        Thread.sleep(3000);
        try {
            driver.findElement(By.xpath("//*[contains(.,'" + EmailExisted_MESSAGE + "')]"));
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    public void setSignupBirthday(String birthday) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(birthday);
        PageOperation.SendKeyToElementAfterWait(driver, "dob", birthday);
    }

    public void setRegisterCountry(String country) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(country);
        String cssSelector = "div.portal-ux-dropdown";
        driver.findElement(By.cssSelector(cssSelector + " > div.portal-ux-dropdown-selection > span")).click();
        String cssSelectorValue = String.format("a[value=\"%s\"] > span:visible", country);
        JavascriptExecutor jse = (JavascriptExecutor) driver;
        jse.executeScript("$('" + cssSelectorValue + "').click()");
    }

    public void setCaptcha(String value) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(value);
        PageOperation.SendKeyToElementAfterWait(driver, "recaptcha", value);
    }

    public void checkboxReadAccept(boolean check) {
        driver.switchTo().window(this.operationWindowHandle);
        WebElement checkbox = driver.findElement(By.id(readAcceptID));
        WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].parentNode;", checkbox);
        if (check && !checkbox.isSelected()) {
            parent.click();
        } else if (!check && checkbox.isSelected()) {
            parent.click();
        }
    }

    public void checkboxContactMe(boolean check) {
        driver.switchTo().window(this.operationWindowHandle);
        WebElement checkbox = driver.findElement(By.id(contactMeID));
        WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].parentNode;", checkbox);
        if (check && !checkbox.isSelected()) {
            parent.click();
        } else if (!check && checkbox.isSelected()) {
            parent.click();
        }
    }

    public void checkboxShareInfo(boolean check) {
        driver.switchTo().window(this.operationWindowHandle);
        WebElement checkbox = driver.findElement(By.id(shareInfoID));
        WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].parentNode;", checkbox);
        if (check && !checkbox.isSelected()) {
            parent.click();
        } else if (!check && checkbox.isSelected()) {
            parent.click();
        }
    }

    public void setSecurityQuestion(String question) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(question);
        String cssSelector = "div.portal-ux-dropdown";
        question = question.replaceAll("'","\\\\'").replaceAll("\"","\\\\\"");
        driver.findElement(By.cssSelector(cssSelector + " > div.portal-ux-dropdown-selection > span")).click();
        String cssSelectorValue = String.format("a[value=\"%s\"] > span:visible", question);
        JavascriptExecutor jse = (JavascriptExecutor) driver;
        jse.executeScript("$('" + cssSelectorValue + "').click()");
    }

    public String getEmailInvalidError() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        return getErrorMessageByXPath("//div[@id='email-status']//div");
    }

    public String getPasswordInvalidError() {
        driver.switchTo().window(this.operationWindowHandle);
        return getErrorMessageByXPath("//div[@id='password-status']//div");
    }

    public String getOriginIdInvalidError() {
        driver.switchTo().window(this.operationWindowHandle);
        String errorMessage=getErrorMessageByXPath("//div[@id='originId-status']//div");
        if (errorMessage.isEmpty()){
            errorMessage=getErrorMessageByXPath("//div[@id='originId-tooltip']//div");
        }
        return getErrorMessageByXPath("//div[@id='originId-status']//div");
    }

    public String getDOBInvalidError() {
        driver.switchTo().window(this.operationWindowHandle);
        return getErrorMessageByXPath("//div[@id='dob-status']//div");
    }
    public String getRecaptchaInvalidError() {
        driver.switchTo().window(this.operationWindowHandle);
        return getErrorMessageByXPath("//div[@id='recaptcha-status']//div");
    }
    public String getCheckingTosError() {
        driver.switchTo().window(this.operationWindowHandle);
        return getErrorMessageByXPath("//div[@id='panel-error-readAccept']//div");
    }
    public String getSecurityQuestionError() {
        driver.switchTo().window(this.operationWindowHandle);
        return getErrorMessageByXPath("//div[@id='panel-error-securityQuestion']//div");
    }
    public String getSecurityQuestionAnswerError() {
        driver.switchTo().window(this.operationWindowHandle);
        return getErrorMessageByXPath("//div[@id='securityAnswer-status']//div");
    }
    public void validateSecurityQuestionAnswerError(String answer, String errorMessage) throws Exception {
        log.info("Validate answer:" + answer);
        setSecurityAnswer(answer);
        driver.findElement(By.cssSelector(".btn-profile>span")).click();
        Assert.assertEquals(getSecurityQuestionAnswerError(), errorMessage, "Input not trigger the correct error message");
    }
    public void validateCheckingTos(boolean checked, String errorMessage) throws Exception {
        log.info("Validate Checked:" + checked);
        checkboxReadAccept(checked);
        driver.findElement(By.cssSelector(".btn-create>span")).click();
        Assert.assertEquals(getCheckingTosError(), errorMessage, "Input not trigger the correct error message");
    }
    public void validateRecaptcha(String recaptcha, String errorMessage) throws Exception {
        log.info("Validate Recaptcha:" + recaptcha);
        sendInput(RecaptchaInputBox, recaptcha);
        driver.findElement(By.cssSelector(".btn-create>span")).click();
        Assert.assertEquals(getRecaptchaInvalidError(), errorMessage, "Input not trigger the correct error message");
    }
    public void validateDOB(String dob, String errorMessage) throws Exception {
        log.info("Validate birthday:" + dob);
        sendInput(DobInputBox, dob);
        driver.findElement(By.cssSelector(".btn-create>span")).click();
        Assert.assertEquals(getDOBInvalidError(), errorMessage, "Input not trigger the correct error message");
    }
    public void validateUnderageDOB(String dob, String errorMessage) throws Exception {
        log.info("Validate birthday:" + dob);
        sendInput(DobInputBox, dob);
        driver.findElement(By.cssSelector(".btn-create>span")).click();
        TestUtil.sleep(2);
        Assert.assertEquals(getErrorMessageByXPath(".//*[@id='panel-error-underage']/div/div"), errorMessage, "Input not trigger the correct error message");
    }
    public void validateOriginId(String OriginId, String errorMessage) throws Exception {
        log.info("Validate OriginId:" + OriginId);
        sendInput(OriginIdInputBox, OriginId);
        clickBlank(".//*[@id='createForm']/div[1]/div[1]");
        driver.findElement(By.cssSelector(".btn-create>span")).click();
        TestUtil.sleep(1);
        Assert.assertEquals(getOriginIdInvalidError(), errorMessage, "Input not trigger the correct error message");
    }

    public void validateEmail(String email, String errorMessage) throws Exception {
        log.info("Validate Email:" + email);
        sendInput(emailInputBox, email);
        this.clickToSignupBasicS1();
        Assert.assertEquals(getEmailInvalidError(), errorMessage, "Input not trigger the correct error message");
    }

    public void validatePassword(String password, String errorMessage) throws Exception {
        log.info("Validate Password:" + password);
        sendInput(passwordInputBox, password);
        driver.findElement(By.cssSelector(".btn-create>span")).click();
        Assert.assertEquals(getPasswordInvalidError(), errorMessage, "Input not trigger the correct error message");
    }

    public boolean verifyElementExist(By by) throws Exception {
        log.info("verify Element:" + by.toString());
        driver.switchTo().window(this.operationWindowHandle);
        try {
            driver.findElement(by);
        } catch (NoSuchElementException e) {
            return false;
        }
        return driver.findElement(by).isDisplayed();
    }

    public void clickBlank(String xpath){
        WebElement element = driver.findElement(By.xpath(xpath));
        Actions actions = new Actions(driver);
        actions.moveToElement(element).click().perform();
        TestUtil.sleep(1);
    }
}
